package com.aj.service;

import com.aj.Interface.UserService;


import com.aj.dao.MenuDao;
import com.aj.dao.PermissionDao;
import com.aj.dao.RoleDao;
import com.aj.dao.UserDao;
import com.aj.pojo.CheckGroup;
import com.aj.pojo.Menu;
import com.aj.pojo.Permission;
import com.aj.pojo.Role;
import com.aj.pojo.User;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import entity.QueryPageBean;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permission;

    @Autowired
    private MenuDao menuDao;

    /**
     * 根据用户名查询
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        User user =  userDao.findByUsername(username);
        if (user!=null){
            Integer id = user.getId();
         Set<Role> rolelist = roleDao.findRoleByUserId(id);

         if (rolelist!=null&&rolelist.size()>0){
             for (Role role : rolelist) {
                 Integer roleId = role.getId();
              Set<Permission> permission2 = permission.findByRoleId(roleId);
              role.setPermissions(permission2);
             }
         }
         user.setRoles(rolelist);

        }
        return user;
    }

   
    public void adduser(User user, Integer[] roleIds) {
        //往user表添加用户信息

        userDao.adduser(user);

        //往user_role添加roleIds
        addUserAndRole(user,roleIds);

    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findpage(QueryPageBean queryPageBean) {

            Integer currentPage = queryPageBean.getCurrentPage();
            PageHelper.startPage(currentPage,queryPageBean.getPageSize() );
            Page<User> page = userDao.findpage(queryPageBean.getQueryString());
            return new PageResult(page.getTotal(),page.getResult());
        }

    /**
     * 根据id删除检查组
     * @return
     */
    @Override
    public void deleteUser(Integer id) {
        userDao.deleteUserAndRole(id);
        userDao.deleteUser(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public User finduserByid(Integer id) {
        User userByid = userDao.finduserByid(id);
        return userByid;
    }


    /**
     * 根据UserId查询角色id
     * @param id
     * @return
     */
    @Override
    public List<Integer> roleIdsByUserId(Integer id) {
        List<Integer> ids = userDao.roleIdsByUserId(id);
        return ids;
    }

    /**
     * 修改用户
     * @param
     * @param
     */
    @Override
    public void update(User user, Integer[] roleIds) {
        userDao.deleteUseridAndroleid(user.getId());
        userDao.update(user);
        addUserAndRole(user,roleIds );
    }


    //往user_role添加roleIds
    private void addUserAndRole(User user, Integer[] roleIds) {
        Integer userid = user.getId();
        for (Integer roleid : roleIds) {
            HashMap map = new HashMap();
            map.put("userid",userid );
            map.put("roleid", roleid);
            userDao.addUserAndRole(map);
        }
    }
	
 //根据用户名获得菜单列表

    public List<Menu> getMenuList(String username) {
        try {
            //获得用户id
            User user = userDao.findByUsername(username);
            //查询用户的角色
            Set<Role> roles = roleDao.findRoleByUserId(user.getId());

            List<Menu> menus = new ArrayList<>();

            for (Role role : roles) {
                //拿到角色对应的父菜单
                Set<Menu> parentMenu = menuDao.findParentMenuByRoleId(role.getId());
                menus.addAll(parentMenu);
                //拿到父菜单下的子菜单
                for (Menu menu : menus) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("parentId",menu.getId());
                    map.put("roleId",role.getId());
                    List<Menu> menuList = menuDao.findMenuByParentId(map);
                    menu.setChildren(menuList);
                }
            }

            for (Menu menu : menus) {
                List<Menu> children = menu.getChildren();
                children.sort(new Comparator<Menu>() {
                    @Override
                    public int compare(Menu o1, Menu o2) {
                        return o1.getPriority()-o2.getPriority();
                    }
                });
            }
            menus.sort(new Comparator<Menu>() {
                @Override
                public int compare(Menu o1, Menu o2) {
                    return o1.getPriority()-o2.getPriority();
                }
            });

            System.out.println(menus);
            return menus;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
